var dialogId;
var editIndex = undefined;
var operateClassId;	//权限分类ID
var operateClassListStr;	//操作权限集合json串
var operateClassList;		//操作权限集合
var cbbAuthResourceId;		//权限资源信息表ID
var targetType;				//权限设置目标类型
var targetId;				//权限设置目标ID
var authServiceId;			//服务ID
var resCode;				//所选档案分类树节点ID
var resName;				//所选择档案分类树节点名称
var actionType;				//操作类型
var operateAuth;			//操作名称
var relTargetIds = "";				//已设置的操作权限
var relTargetNames = "";				//已设置的操作权限名称串
var sysId = "";//系统ID;

$(function(){
	loadTree();
	if(operateClassListStr != null && operateClassListStr != ""){
		operateClassList=eval(operateClassListStr);
	}
});

// 初始化档案分类树
function loadTree() {
	$('#mainTree').tree({
		checkbox : false,
		lines : true,
		dnd : false,
		method : 'post',
		dataType : 'json',
		loadFilter : function(data) {
			if(operateClassId == null || operateClassId == ""){
				return "";
			}
			// 过滤后台返回的数据，处理不同json结构
			if (data.data) {
				return data.data;
			} else {
				return data;
			}
		},
		onClick : function(node) {
			
		},
		formatter:function(node){
			if(node.attributes.IS_OPERATE_BTN == "Y"){
				var c = "";
				if(node.attributes.IS_CONTROL == "Y"){	//节点按钮节点
					c = "<input type='checkbox' value='Y' id='"+node.id+"' checked='checked' onclick='' />"+ '<span title="' + node.text + '">' + node.text + '</span>';
				}
				else{
					c = "<input type='checkbox' value='N' id='"+node.id+"' onclick='' />"+ '<span title="' + node.text + '">' + node.text + '</span>';
				}
				return c;
			}
			return  '<span title="' + node.text + '">' + node.text + '</span>';
		}
	});
}

// 使目标指标信息的必保值和挑战值可以编辑
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var fields = $("#operateDatagrid").datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $("#operateDatagrid").datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$("#operateDatagrid").datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $("#operateDatagrid").datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});

//双击grid单元格事件
function operateOnDbClickCell(index, field, value){
//	var row = $('#operateDatagrid').datagrid("getSelected");
//	if(row.ID == null || row.ID == ""){
//		if (endEditing()){
//			if(field == 'OPERATE_CLASS_NAME'){
//				$('#operateDatagrid').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
//				editIndex = index;
//				var ed = $(this).datagrid('getEditor', {index:index,field:field});
//				$(ed.target).focus();
//			}
//		}
//	}
}

//编辑单元格
function endEditing(){
	if (editIndex == undefined){
		return true;
	}
	if ($('#operateDatagrid').datagrid('validateRow', editIndex)){
		$('#operateDatagrid').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}

/**
 * 添加
 */
function addOperateClass(){
	//$('#operateDatagrid').datagrid('endEdit', editIndex);
	var rowCount = $("#operateDatagrid").datagrid("getRows").length;
	if(rowCount == null || rowCount == ""){
		rowCount = -1;
	}
	
	//添加一行记录
	$('#operateDatagrid').datagrid('insertRow',{
		index: rowCount + 1,
		row: {
		}
	});

}

/**
 * 
 * @param rowIndex
 * @param rowData
 */
function operateOnClickRow(rowIndex, rowData){
	if(rowData.ID == null || rowData.ID == ""){
		operateClassId = rowData.OPERATE_CLASS_NAME;	//未保存记录分类名称实际值为ID
	}
	else{
		operateClassId = rowData.ID;
	}
	
	//刷新右侧树
	$("#mainTree").tree("options").url = "platform/UserAuthSetController/getMenu?operateClassId="+operateClassId;
	$('#mainTree').tree('reload');
}

/**
 * 将权限咧转化为复选框
 */
function formatClassName(value, rowData, rowIndex) {
	var val = value;
	if(operateClassList != null){
		for(var i = 0; i < operateClassList.length; i++){
			if(value == operateClassList[i].id){
				val = operateClassList[i].text;
				break;
			}
		}
	}
	return val;
}

/**
 * 保存操作权限设置
 */
function saveOperateAuthSet(){
	$('#operateDatagrid').datagrid('endEdit', editIndex);
	var checkedRows = $("#operateDatagrid").datagrid("getChanges");
	var targetIds = "";				//已选择操作权限ID串
	if(checkedRows.length > 0){
		for(var i = 0; i < checkedRows.length; i++){
			targetIds += checkedRows[i].OPERATE_CLASS_NAME + ",";
		}
		$.ajax({
			url : 'platform/UserAuthSetController/saveBatchOperateAuthSet_new',
			data : {
				cbbAuthResourceId : cbbAuthResourceId,
				targetType : targetType,
				targetId : targetId,
				targetIds : targetIds,
				authServiceId : authServiceId,
				resCode : resCode,
				resName : resName,
				actionType : actionType,
				operateAuth : operateAuth,
				sysId : sysId
				},
			type : 'post',
			dataType : 'json',
			success : function(result) {
				parent.batchOperateAuthSetSaveAfter(result.flag);
				closeWindow();
			}
		});
	}else{
		//commonLang.tip.alert替换'提示',commonLang.optip.save.selectRole替换'请选择角色！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.selectRole, 'warning');
	}
}

/**
 * 返回
 */
function closeWindow(){
	if (parent != null && parent.$('#' + dialogId) != null) {
		parent.$('#' + dialogId).dialog('close');
	}
}

/**
 * 窗口关闭事件，刷新父页面操作角色列表
 */
window.onbeforeunload = function(){
	parent.operateRoleSetAfter();
};

/**
 * 单击grid单元格事件
 * @param index
 * @param field
 * @param value
 */
function operateOnClickCell(index, field, value){
	//空记录可以添加
	if(value == null || value == ""){
		
		operateClassId = "";	//进入编辑器清空行ID
		$("#mainTree").tree("options").url = "platform/UserAuthSetController/getMenu?operateClassId="+operateClassId;
		$('#mainTree').tree('reload');
		
		if (endEditing()){
			if(field == 'OPERATE_CLASS_NAME'){
				$('#operateDatagrid').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
				editIndex = index;
				var ed = $(this).datagrid('getEditor', {index:index,field:field});
				$(ed.target).focus();
			}
		}
	}
}

/**
 * 单击浏览器非控件内容任意位置后，关闭编辑器。保存数据
 */
function bodyOnClick(){
	$('#operateDatagrid').datagrid('endEdit', editIndex);
}